<?php
$starttime = microtime(1);
//setlocale(LC_ALL, 'zh_CN.UTF-8');
header("Content-Type:text/html; charset=utf-8");
date_default_timezone_set('Asia/Shanghai');

define('IN_ROOT', true);
define('GODHOUSE_ROOT', str_replace('\\', '/', getcwd()).'/');

require GODHOUSE_ROOT.'library/config.global.php';
require GODHOUSE_ROOT.'library/config.php';
require GODHOUSE_ROOT.'library/function.php';

error_reporting(GODHOUSE_DEBUG ? E_ALL : 0);

GODHOUSE_DEBUG && set_error_handler("godhouseErrorHandle");

// 清除变量
unset($_GET, $GLOBALS, $_ENV, $HTTP_GET_VARS, $HTTP_POST_VARS, $HTTP_COOKIE_VARS, $HTTP_SERVER_VARS, $HTTP_ENV_VARS);

// 初始化 $_GET
//$requestURL = $_SERVER['HTTP_X_REWRITE_URL'];
$requestURL = $_SERVER['REQUEST_URI'];

$module = '';
if(strpos($requestURL, '/', 1)!==FALSE) {
	$requestURL = substr($requestURL, 1);
	$q = strpos($requestURL, '?');
	if($q!==FALSE) {
		$arr = explode('?', $requestURL);
		if(strpos($arr[0], '/')!==FALSE) {
			$arr2 = explode('/', $arr[0]);
			$requestURL = $arr2[1]."?".$arr[1];
			$module = $arr2[0];
			unset($arr, $arr2);
		}
	} else {
		$arr = explode('/', $requestURL);
		$requestURL = $arr[1];
		$module = $arr[0];
		unset($arr);
	}
	unset($q);
} else {
	$requestURL = substr($requestURL, 1);
}
$_GET = explode('-', substr($requestURL, 0, strpos($requestURL, '.htm')));
if(strpos($requestURL, '?') !== FALSE) {
	$arr = (array)parse_url($requestURL);
	isset($arr['query']) && parse_str($arr['query'], $arr);
	$_GET += $arr;
	$_REQUEST += $arr;
	unset($arr);
}
$_GET['m'] = $module;
$_GET['c'] = isset($_GET[0]) && !is_numeric($_GET[0]) && preg_match("/^[a-z\d]{2,22}$/", $_GET[0]) ? $_GET[0] : 'index';
$_GET['a'] = isset($_GET[1]) && preg_match("/^[a-z_A-Z_\d]{2,50}$/", $_GET[1]) ? $_GET[1] : 'index';
unset($module, $requestURL);

require GODHOUSE_ROOT.'library/godhouse.php';
require GODHOUSE_ROOT.'library/base.php';
godhouse::import();

if($_GET['m']) {
	$controlPath = GODHOUSE_ROOT.'application/controllers/'.$_GET['m'].'/'.ucfirst($_GET['c']).'Controller.php';
} else {
	$controlPath = GODHOUSE_ROOT.'application/controllers/'.ucfirst($_GET['c']).'Controller.php';
}
//print_r($_GET);exit;
if(file_exists($controlPath)) {
	require $controlPath;
	$className = ucfirst($_GET['c']).'Controller';
	$control = new $className();
	$method = 'action'.ucfirst($_GET['a']);
	if(method_exists($control, $method)) {
		$control->init();
		$control->$method();
	} else {
		exit('Action not found!');
	}
} else {
	exit('Controller not found!');
}

if(!getgpc('inajax') && GODHOUSE_DEBUG) {
	echo '<script>if(document.getElementById("debug_time"))document.getElementById("debug_time").innerHTML = "'.substr((microtime(1) - $starttime), 0, 6).'";</script>';
}